/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gui;

import Params.ServerSocketItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;

/**
 *
 * @author ANH PHONG
 */
 
public class frmSendMessage extends javax.swing.JInternalFrame implements ActionListener {
    Socket _sockdangchon = null;
    /**
     * Creates new form frmSendMessage
     */
    public frmSendMessage() {
        initComponents();
    }
 public frmSendMessage(Socket client) {
        initComponents();
        this.jtxtmessage.addActionListener(this);
        _sockdangchon = client;
        int temp = (client.getPort());
        this.title = "SERVER - CLIENT " + temp + "";
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LABEL1 = new javax.swing.JLabel();
        jbtnsend = new javax.swing.JButton();
        jtxtmessage = new javax.swing.JTextField();

        setTitle("Thông Báo ");

        LABEL1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        LABEL1.setText("Nội Dung Thông Báo :");

        jbtnsend.setText("Send");
        jbtnsend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnsendActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jtxtmessage))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(LABEL1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(152, 152, 152)
                                .addComponent(jbtnsend)))
                        .addGap(0, 165, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(LABEL1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtmessage, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnsend)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnsendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnsendActionPerformed
        // TODO add your handling code here:
        // TODO add your handling code here:

        for (int i = 0; i < MainSever.listConnection.size(); i++) {
            if (!this.jtxtmessage.getText().equals("")) {
                ServerSocketItem temp = (ServerSocketItem) MainSever.listConnection.elementAt(i);
                if (temp.getConnectionMessages().connectionSocket != null) {
                    if (temp.getSOCKM().getPort() == (_sockdangchon.getPort())+1) {
                        temp.getConnectionMessages().bufferedWriter.println(this.jtxtmessage.getText().toString());
                        temp.getConnectionMessages().bufferedWriter.flush();
                    }
                }
            }
        }
        this.dispose();
    }//GEN-LAST:event_jbtnsendActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LABEL1;
    private javax.swing.JButton jbtnsend;
    private javax.swing.JTextField jtxtmessage;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < MainSever.listConnection.size(); i++) {
            if (!this.jtxtmessage.getText().equals("")) {
                ServerSocketItem temp = (ServerSocketItem) MainSever.listConnection.elementAt(i);
                if (temp.getConnectionMessages().connectionSocket != null) {
                    if (temp.getSOCKM().getPort() == (_sockdangchon.getPort())+1) {
                        temp.getConnectionMessages().bufferedWriter.println(this.jtxtmessage.getText().toString());
                        temp.getConnectionMessages().bufferedWriter.flush();
                    }
                }
            }
        }
        this.dispose();
    }
}
